home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / Snippets / Interapplication Communication / AECDEV⁄AEDAEMON / AECdev.c next >
Encoding:
C/C++ Source or Header  |  1995-02-09  |  10.0 KB  |  216 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------
  2. #
  3. #   Apple Developer Technical Support
  4. #
  5. #   AppleEvent Sample Control Panel Device
  6. #
  7. #   AECDEV
  8. #
  9. #   AECDEV.c  -   C Source
  10. #   Written by  C.K. Haun
  11. #
  12. #   Copyright © 1991 Apple Computer, Inc.
  13. #   All rights reserved.
  14. #
  15. #   Versions:   1.0                 8/91
  16. #
  17. #   Components: AECdev.p          August 2, 1991
  18. #               AECDEV.PPC.c      August 2, 1991
  19. #
  20. #   AECDEV demonstrates the techniques needed to send AppleEvents
  21. #   from a CDEV/DA/INIT/Driver.
  22. #   Requires the sample AEDaemon to work.
  23. #   
  24. #   The techniques used in AECdev are the same you need to use to send AppleEvents
  25. #   from any non-application piece of code, DAs/INIT/Drivers, or anything else.
  26. #   Since the High Level Event manager will not let anyone post High Level Events 
  27. #   unless they have an event loop, your non-event looped code must rely
  28. #   on a 'buddy' (in this case AEDaemon) to do the sending for you.
  29. #   There's not really much more code to write (just some PPC code), it's
  30. #   mainly a matter of moving the code from the CDEV to the backgrounder.
  31. #
  32. #   Using a buddy and PPC can give you all the functionality of AppleEvents.
  33. ------------------------------------------------------------------------------*/
  34. /*------------------------------------------------------------------------------
  35. #   This file contains the basic CDEV controlling code
  36. ------------------------------------------------------------------------------*/
  37.  
  38. #include "AECdev.h"
  39.  
  40. /* This is the main dispatcher. It must be the first code in the cdev.
  41. AECDEV's dispatcher responds only to the following messages from
  42. the Control Panel:
  43.  
  44. macDev      - To indicate what machines it is available on.
  45. initDev     - To set up some temporary storage and get the caret started.
  46.  
  47. */
  48. pascal Handle AECDEV(short message, short item, short numItems, short CPanelID, EventRecord *theEvent, Handle cdevStorage,
  49.                      DialogPtr CPDialog)
  50. {
  51.     CDEVPtr temp;
  52. #pragma unused (CPanelID,theEvent)     /* unused formal parameters */    
  53.     if (message == macDev)
  54.         return((Handle)1);                                  /* we work on every machine */
  55.     else if (cdevStorage != nil) {
  56.         switch (message) {
  57.             long result;
  58.             case initDev:                                   /* initialize cdev */
  59.                 cdevStorage = NewHandle(sizeof(CDEVRec));       /* create private storage */
  60.                 /* get a PPC block.  If we can't allocate one, we'll just be dead */
  61.                 if (cdevStorage) {
  62.                     HLock(cdevStorage);
  63.                     temp = (CDEVPtr)*cdevStorage;
  64.                     temp->myPPCBlock = (MyPPCRecPtr)NewPtrClear(sizeof(MyPPCRec));
  65.                     if (temp->myPPCBlock) {
  66.                         temp->myPPCBlock->myPort = (PPCPortPtr)NewPtrClear(sizeof(PPCPortRec));
  67.                         if (temp->myPPCBlock->myPort) {
  68.                         /* set the default settings for my blocks */
  69.                             temp->myPPCBlock->buffer = nil;
  70.                             temp->myPPCBlock->bufferSize = nil;
  71.                             temp->myPPCBlock->ourPort = nil;
  72.                             temp->myPPCBlock->dataToXfer = nil;
  73.                             temp->myPPCBlock->currentSessionRef = nil;
  74.                             temp->searchForTarget = false;
  75.                             temp->noBuddy = false;
  76.                             temp->notSys7 = false;
  77.                             temp->eventPending = false;
  78.                             HUnlock(cdevStorage);
  79.                             HiliteControl(SnatchHandle(CPDialog, numItems + 1), 255);
  80.                             /* check Gestalt to see if we're on an AppleEvent/PPC */
  81.                             /* capable machine machine */
  82.                             if ((Gestalt(gestaltAppleEventsAttr, &result) != noErr) ||
  83.                                 (Gestalt(gestaltPPCToolboxAttr, &result) != noErr)) {
  84.                                 /* no PPC or no AppleEvents.  Forget it */
  85.                                 temp = (CDEVPtr)*cdevStorage;
  86.                                 temp->notSys7 = true;
  87.                             } else {
  88.                             /* Start PPC for us */
  89.                                 if (PPCInit() == noErr) {
  90.                                     FindAEBuddy((CDEVHnd)cdevStorage);/* find and launch AEBud */
  91.                                 } else {
  92.                                 /* PPC didn't init correctly, bail out */
  93.                                     temp = (CDEVPtr)*cdevStorage;
  94.                                     temp->notSys7 = true;
  95.                                 }
  96.                             }
  97.                             temp = (CDEVPtr)*cdevStorage;
  98.                             /* Alert user that we con't find our Buddy */
  99.                             if (temp->noBuddy)
  100.                                 StopAlert(kNoBuddyAlert, nil);
  101.                         } else {                            /* PPC Port rec mem check */
  102.                             /* if this fails, kill the other pointer also */
  103.                             DisposPtr((Ptr)temp->myPPCBlock);
  104.                             temp->myPPCBlock = nil;
  105.                             DisposHandle(cdevStorage);
  106.                             /* and have Control Panel manager say out of mem */
  107.                             cdevStorage = nil;
  108.                         }
  109.                     } else {                                /* block mem check */
  110.                         DisposHandle(cdevStorage);
  111.                         /* and have Control Panel manager say out of mem */
  112.                         cdevStorage = nil;
  113.                     }
  114.                 }                                           /* cdevStorage mem check */
  115.                 break;
  116.             case nulDev:
  117.                 /* check here all the time */
  118.                 /* for the port coming up if we had to launch the sender */
  119.                 /* also dim the control, as necessary */
  120.                 /* Can't do this in our completion routine, since it requires calling the */
  121.                 /* COntrol Manager and the Memory Manager */
  122.                 HLock((Handle)cdevStorage);
  123.                 temp = (CDEVPtr)*cdevStorage;                
  124.                 if (temp->eventPending || (temp->searchForTarget) || temp->myPPCBlock->currentSessionRef) {
  125.                     HiliteControl(SnatchHandle(CPDialog, numItems + kSendButton), 255);
  126.                     if (!temp->notSys7)
  127.                         FindAEBuddy((CDEVHnd)cdevStorage);
  128.                 } else {
  129.                     /* if the buddy is not up yet, or if it could not be found at all */
  130.                     /* dim the button out. */
  131.                     if (temp->noBuddy || temp->notSys7)
  132.                         HiliteControl(SnatchHandle(CPDialog, numItems + kSendButton), 255);
  133.                     else
  134.                         HiliteControl(SnatchHandle(CPDialog, numItems + kSendButton), 0);
  135.                 }
  136.                 /* also, there's .... I forgot what I was going to say */
  137.                 /* Oh yeah, I remember.  I also need to check here periodically to */
  138.                 /* see if the PPC calls have completed so I can dispose of the */
  139.                 /* data handle that I allocated for the PPC XFer process. */
  140.                 if (!temp->myPPCBlock->pB.openParam.portRefNum && temp->myPPCBlock->buffer != nil && !temp->noBuddy) {
  141.                     DisposPtr(temp->myPPCBlock->buffer);
  142.                     temp->myPPCBlock->buffer = nil;
  143.                     temp->eventPending = false;
  144.                 }
  145.                 HUnlock((Handle)cdevStorage);
  146.                 break;
  147.                 
  148.             case hitDev:   /* handle hit on item */
  149.             /* They clicked in our button.  Find our Buddy ( it is possible that it  */
  150.             /* terminated while we were waiting, this will re-launch it if that happened ) */
  151.             
  152.                 if ((item - numItems) == kSendButton) {
  153.                     /* our send button.  Do it now */
  154.                     FindAEBuddy((CDEVHnd)cdevStorage);                    
  155.                     FindATarget((CDEVHnd)cdevStorage);
  156.                 }
  157.                 break;
  158.             case closeDev:                                  /* clean up and dispose */
  159.                 temp = (CDEVPtr)*cdevStorage;
  160.                 if (temp->myPPCBlock->ourPort) {
  161.                     /* we have a port open.  Close the port, blowing off any pending */
  162.                     /* writes or whatever */
  163.                     PPCClosePBPtr closeRec = NewPtrClear(sizeof(PPCClosePBRec));
  164.                     closeRec->ioCompletion = nil;
  165.                     closeRec->ioResult = 0;
  166.                     closeRec->portRefNum = temp->myPPCBlock->ourPort;
  167.                     PPCClose(closeRec, false);
  168.                     DisposPtr((Ptr)closeRec);
  169.                                 }
  170.                     /* and kill our memory */
  171.                     if (temp->myPPCBlock->myPort)DisposPtr((Ptr)temp->myPPCBlock->myPort);
  172.                     if (temp->myPPCBlock)DisposPtr((Ptr)temp->myPPCBlock);
  173.                     
  174.                 break;
  175.                 
  176.             case updateDev:                                 /* handle any update drawing */
  177.             case activDev:                                  /* activate any needed items */
  178.             case deactivDev:                                /* deactivate any needed items */
  179.                 break;
  180.                 
  181.             case keyEvtDev:                                 /* respond to keydown */
  182.                 break;
  183.                 
  184.             case macDev:
  185.             case undoDev:
  186.                 break;
  187.                 
  188.             case cutDev:
  189.             case copyDev:
  190.             case pasteDev:
  191.             case clearDev:
  192.                 break;
  193.         }
  194.         return(cdevStorage);
  195.     }  /* cdevStorage != nil */
  196.     
  197.     /* 
  198.     **  if cdevStorage = NIL then ControlPanel 
  199.     **  will put up memory error
  200.     */
  201.     return(nil);
  202. }
  203.  
  204. /* Gets the ControlHandle for the item you want in the dialog box thebox.  */
  205. /* Handy for setting checkboxes and radio buttons */
  206. ControlHandle SnatchHandle(DialogPtr thebox, short theGetItem)
  207. {
  208.     short itemtype;
  209.     Rect itemrect;
  210.     Handle thandle;
  211.     GetDItem(thebox, theGetItem, &itemtype, &thandle, &itemrect);
  212.     return((ControlHandle)thandle);
  213. }
  214.  
  215.  
  216.